<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

/** ------------------------------------------
 *  Route model binding
 *  ------------------------------------------
 */
Route::model('user', 'User');
Route::model('entrada', 'Entrada');
Route::model('url', 'Url');
Route::model('role', 'Role');
Route::model('evento', 'Evento');
Route::model('carpeta', 'Carpeta');

/** ------------------------------------------
 *  Route constraint patterns
 *  ------------------------------------------
 */
Route::pattern('entrada', '[0-9]+');
Route::pattern('url', '[0-9]+');
Route::pattern('evento', '[0-9]+');
Route::pattern('carpeta', '[0-9]+');
Route::pattern('user', '[0-9]+');
Route::pattern('role', '[0-9]+');
Route::pattern('token', '[0-9a-z]+');

/** ------------------------------------------
 *  Admin Routes
 *  ------------------------------------------
 */
Route::group(array('prefix' => 'admin', 'before' => 'auth'), function()
{

  

    # User Management
    Route::get('users/{user}/show', 'AdminUsersController@getShow');
    Route::get('users/{user}/edit', 'AdminUsersController@getEdit');
    Route::post('users/{user}/edit', 'AdminUsersController@postEdit');
    Route::get('users/{user}/delete', 'AdminUsersController@getDelete');
    Route::post('users/{user}/delete', 'AdminUsersController@postDelete');
    Route::controller('users', 'AdminUsersController');
	
    # Entrada Management
    Route::get('entradas/{entrada}/show', 'AdminEntradasController@getShow');
    Route::get('entradas/{entrada}/edit', 'AdminEntradasController@getEdit');
    Route::post('entradas/{entrada}/edit', 'AdminEntradasController@postEdit');
    Route::get('entradas/{entrada}/delete', 'AdminEntradasController@getDelete');
    Route::post('entradas/{entrada}/delete', 'AdminEntradasController@postDelete');
    Route::controller('entradas', 'AdminEntradasController');

	# Entrada Management
	Route::get('eventos/{evento}/show', 'AdminEventosController@getShow');
	Route::get('eventos/{evento}/edit', 'AdminEventosController@getEdit');
	Route::post('eventos/{evento}/edit', 'AdminEventosController@postEdit');
	Route::get('eventos/{evento}/delete', 'AdminEventosController@getDelete');
	Route::post('eventos/{evento}/delete', 'AdminEventosController@postDelete');

	Route::controller('eventos', 'AdminEventosController');
    
    # User Role Management
    Route::get('roles/{role}/show', 'AdminRolesController@getShow');
    Route::get('roles/{role}/edit', 'AdminRolesController@getEdit');
    Route::post('roles/{role}/edit', 'AdminRolesController@postEdit');
    Route::get('roles/{role}/delete', 'AdminRolesController@getDelete');
    Route::post('roles/{role}/delete', 'AdminRolesController@postDelete');
    Route::controller('roles', 'AdminRolesController');

    # Admin Dashboard
    Route::controller('/', 'AdminDashboardController');
});
/** -----------------------------------------
 * Dropzone Route
 */
// upload file
Route::post('upload', function () {

	$file = Input::file('file');
    $entrada = Entrada::find(Input::get('entrada'));
	if($file) {

		$destinationPath = public_path() . '/entradas/';
		$filename = $file->getClientOriginalName();
		$marca = round(microtime(true));
		$extension = preg_split("/\./",$filename);
		$extension = $extension[sizeof($extension)-1];
		$upload_success = Input::file('file')->move($destinationPath, $marca.'.'.$extension);
		
		if ($upload_success) {
			$enlace = new Enlace;
			$enlace->nombre = $filename;
			$enlace->url =  $marca.'.'.$extension;
			$enlace->carpeta_id = 1;
			$enlace->tipo = $extension;
			$enlace->save();
			$entrada->urls()->attach($enlace->id);
			// resizing an uploaded file
			if($extension == "jpg"){
			Image::make($destinationPath . $marca.'.'.$extension)->resize(150, 150)->save($destinationPath. "150x150_" . $marca.'.'.$extension);
			}
			return Response::json('success', 200);
		} else {
			return Response::json('error', 400);
		}
	}
});

	// delete image
	Route::post('delete-image', function () {

		$destinationPath = public_path() . '/entradas/';
		if(File::exists($destinationPath . Input::get('file'))){
			File::delete($destinationPath . Input::get('file'));
			File::delete($destinationPath . "150x150_" . Input::get('file'));
			$enlace = Enlace::where("url","=",Input::get('file'))->first();
		}else{
			$enlace = Enlace::where("nombre","=",Input::get('file'))->first();
			File::delete($destinationPath . $enlace->url());
			File::delete($destinationPath . "150x150_" . $enlace->url());
			
		}
		
	
		$entrada = Entrada::find(Input::get('entrada'));
		$entrada->urls()->detach($enlace->id);
		$enlace->delete();
		return Response::json('success', 200);
	});

/** ------------------------------------------
 *  Frontend Routes
 *  ------------------------------------------
 */

// User reset routes
Route::get('user/reset/{token}', 'UserController@getReset');
// User password reset
Route::post('user/reset/{token}', 'UserController@postReset');
//:: User Account Routes ::
Route::post('user/{user}/edit', 'UserController@postEdit');

//:: User Account Routes ::
Route::post('user/login', 'UserController@postLogin');

# User RESTful Routes (Login, Logout, Register, etc)
Route::controller('user', 'UserController');

//:: Application Routes ::

Route::get('entradas/{entrada}/details', 'AdminEntradasController@getDetails');


# Filter for detect language
Route::when('contact-us','detectLang');

Route::get('inicio', function()
{
	$entradas = Entradas::all();
	return View::make('site/welcome',compact('entradas'));
});

Route::get('actividades', function()
{
	$eventos = Evento::all();
	$calendarEvent = array();
	foreach ($eventos as $evento){
		$fecha = new DateTime($evento->fecha_inicio);
		$fechaFin = new DateTime($evento->fecha_fin);

		$calendarEvent[] = array(
			"id" 	=> $evento->id,
			"title" => $evento->titulo,
			"class" => "event-important",
			"url"	=> URL::to('eventos/' . $evento->id . '/details'),
			"start" => strtotime($evento->fecha_inicio).'000',
			"end" 	=> strtotime($evento->fecha_fin).'000',
		);






	}
	JavaScript::put([
		'calendar' => $calendarEvent,

	]);
return View::make('site/actividades');
});

Route::get('eventos/{evento}/details', 'AdminEventosController@getDetails');

Route::get('inicio', function(){
	$eventos = Evento::all();
	$calendarEvent = array();
	foreach ($eventos as $evento){
		$fecha = new DateTime($evento->fecha_inicio);
		$fechaFin = new DateTime($evento->fecha_fin);
		do {
			$calendarEvent[$fecha->format('Y-m-d')] = array(
				"badgeClass" => "badge-warning",
				"url" => URL::to('eventos/' . $evento->id . '/details'),
			);

			$fecha->add(new DateInterval('P1D'));

		}while($fecha <= $fechaFin);


	}
	JavaScript::put([
		'calendarEvent' => $calendarEvent,

	]);


	return View::make('site/welcome');});

Route::get('sagrados-titulares', function(){ return View::make('site/titulares');});

Route::get('servicios', function(){	return View::make('site/services');});

Route::get('noticias', function(){ return View::make('site/notices');});

Route::get('juventud', function(){ return View::make('site/juventud');});

Route::get('historia', function(){ return View::make('site/historia');});

Route::get('reglas-y-ordenanzas', function(){ return View::make('site/reglas-y-ordenanzas');});

Route::get('capilla', function(){ return View::make('site/capilla');});

Route::get('heraldica', function(){	return View::make('site/heraldica');});

Route::get('medalla', function(){ return View::make('site/medalla');});

Route::get('junta-de-gobierno', function(){	return View::make('site/junta-de-gobierno');});

Route::get('obras-asistenciales', function(){	return View::make('site/patronato');});

//PENITENCIA
Route::get('introduccion', function(){	return View::make('site/penitencia-introduccion');});
Route::get('el-cortejo', function(){	return View::make('site/penitencia-cortejo');});
Route::get('los-pasos', function(){	return View::make('site/penitencia-pasos');});
Route::get('las-tunicas', function(){	return View::make('site/penitencia-tunicas');});
Route::get('horarios-e-itinerario', function(){	return View::make('site/penitencia-itinerario');});
//FIN PENITENCIA

//CULTOS
Route::get('misas-diarias', function(){	return View::make('site/cultos-misas-diarias');});
Route::get('misas-de-regla', function(){	return View::make('site/cultos-misa-de-regla');});
Route::get('triduo-eucaristico', function(){	return View::make('site/cultos-triduo-eucaristico');});
Route::get('quinario-al-cristo-de-la-expiracion', function(){	return View::make('site/cultos-quinario-cristo');});
Route::get('quinario-al-cristo-de-la-expiracion', function(){	return View::make('site/cultos-quinario-cristo');});
Route::get('triduo-a-la-virgen-de-las-aguas', function(){	return View::make('site/cultos-triduo-virgen');});
Route::get('misa-de-navidad', function(){	return View::make('site/cultos-misa-navidad');});
Route::get('adoracion-nocturna', function(){	return View::make('site/cultos-adoracion-nocturna');});
Route::get('besapies-y-besamanos', function(){	return View::make('site/cultos-besapies-besamanos');});
Route::get('calendario-de-cultos', function(){	return View::make('site/cultos-calendario-cultos');});
//FIN CULTOS

Route::get('papeletas-de-sitio', function(){	return View::make('site/cultos-papeletas-sitio');});

Route::get('contacto', function(){ return View::make('site/contact');});

Route::get('aviso-legal', function(){ return View::make('site/legal');});

# Contact Us Static Page
Route::get('contact-us', function(){ return View::make('site/contact-us');});

App::missing(function($exception){	return Redirect::action('WelcomeController@getIndex');});

# Index Page - Last route, no matches
Route::get('/', array('after' => 'detectLang','uses' => 'WelcomeController@getIndex'));
